package com.edu.app.service.impl.wechat;

import com.edu.app.model.wechat.WxErrorException;
import com.edu.app.service.sys.wechat.WeChatServiceExecutor;
import com.edu.app.service.sys.wechat.WechatUserService;
import com.edu.app.util.http.DataType;

import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class WechatUserServiceImpl   implements WechatUserService {

    String GET_USERLIST_URL = "https://api.weixin.qq.com/cgi-bin/user/get";

    String GET_USER_INFO = "https://api.weixin.qq.com/cgi-bin/user/info";


    WeChatServiceExecutor executor;

    WechatUserServiceImpl(WeChatServiceExecutor executor){
        this.executor = executor;
    }
    /**
     * 获取用户列表

     * @param nextOpenid
     * @return 状态
     */
    @Override
    public String userList(String nextOpenid) throws WxErrorException {

        //TemplateMsgResult templateMsgResult = null;
        HashMap<String, Object> params = new HashMap<>();
        String response = null;
        try  {
            if (nextOpenid != null)
                params.put("next_openid",nextOpenid);
            response = executor.execute(GET_USERLIST_URL,params,ExecutorType.GETSYNC, DataType.JSON);
            //templateMsgResult = (TemplateMsgResult)JsonUtil.fromJson(response, TemplateMsgResult.class);
        }
        catch(WxErrorException e){
           e.printStackTrace();
        }

        return response;
    }

    @Override
    public String userInfo(String openid) throws WxErrorException {
        return this.userInfo(openid, null);
    }

    @Override
    public String userInfo(String openid, String lang) throws WxErrorException {
        HashMap<String, Object> params = new HashMap<>();
        String response = null;
        lang = lang == null ? "zh_CN" : lang;


        params.put("openid",openid);
        params.put("lang",lang);
        response = executor.execute(GET_USER_INFO,params,ExecutorType.GETSYNC,DataType.JSON);

        return response;
    }
}
